﻿/*
%include "/projects/hsieh_project/proj_201809/code_1_data/data_1_jobcr_0.sas"

Macro that calculates job creation/destruction for the given definition of city and years.

param_city = Definition of city
param_lyear = List of years.
    Assume that the list of years is 1977,1982,1987,1992. Then the job creation between the following combinations of years are calculated:
    1977-1982, 1982-1987, 1987-1992, 1977-1992.
*/

%include "/projects/hsieh_project/proj_201809/code_1_data/m_read.sas" /source2;
%include "/projects/hsieh_project/proj_201809/code_1_data/data_0_jobcr.sas" /source2;

%macro m_jobcr_main(param_city= , param_lyear= ) / minoperator;

libname hr "/projects/hsieh_project/proj_201809/data/";

%Let c_city = &param_city.;

%Global yearf years yearl;
%Let yearf = 1977;
%Let years = 10;
%Let yearl = 2013;
%Global l_year;
%Let l_year=&param_lyear.;

%put CITY: &c_city.;
%put YEAR: &l_year.;

/*
================================================================================
Load Raw Data 
*/

%if "&param_city" in ("msa1983cz" "msa2010cz") %then %do;
%m_read(param_dev=1,param_lyear=0, param_czone=0, param_msa1983=0, param_msacz=1, param_drop=1); /* Merge with msa_czone */
%end;
%else %if "&param_city" in ("msa1983") %then %do;
%m_read(param_dev=1,param_lyear=0, param_czone=0, param_msa1983=1, param_msacz=0, param_drop=1);
%end;
%else %if "&param_city" in ("czone") %then %do;
%m_read(param_dev=1,param_lyear=0, param_czone=1, param_msa1983=0, param_msacz=0, param_drop=1);
%end;
%else %do;
%m_read(param_dev=1,param_lyear=0, param_czone=0, param_msa1983=0, param_msacz=0, param_drop=1);
%end;

/* Keep selected years */
data lbd;
  set lbd;
  if year in (&l_year.);
run;

/*
--------------------------------------------------------------------------------
Generate a new definition of establishments, where we consider the establishment to be the same
if and only if it belongs to the same owner.
*/
data lbd;
  set lbd;
  flbdid=CATS(firmnum,lbdid);
run;

/*
--------------------------------------------------------------------------------
Mark Establishments of top firms
*/

/*
----------------------------------------
Mark Establishments of Top Industry Firm
*/
data lbd;
  set lbd;
  if indf_perc <= 0.1 then indf_10 = 1;
  else indf_10 = 0;
  if indf_n < 10 then indf_10 = .;
run;

/*
----------------------------------------
Mark Establishments of Top City-Industry Firm
*/

/* Find top city-industry firms */
proc sort data=lbd; by year &c_city. indf_rank runif;

data cindf_t1;
  set lbd;
  by year &c_city.;
  if first.&c_city.;
  cindf_t1=1;
  keep year &c_city. firmnum cindf_t1;
run;

proc sort data=lbd; by year &c_city. firmnum;
proc sort data=cindf_t1; by year &c_city. firmnum;

data lbd;
  merge lbd cindf_t1;
  by year &c_city. firmnum;
run;

data lbd;
  set lbd;
  if cindf_t1=. then cindf_t1=0;
run;


/*
================================================================================
Run the main macro for job creation calculation
*/

%jc_main(dt_in=lbd, dt_out=lbd_&c_city., c_city=&c_city., c_ind=ch_ind, c_add=%bquote(indf_10 cindf_t1), c_code=cind_plant, l_year=%bquote(&l_year.), l_def=%bquote(2,4,6));

%mend;

/* End of SAS file */
